<?php

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/7/4 0004
 * Time: 下午 3:39
 */
class ChildCommentModel
{
    private $childCommentsKeyPrefix  =   'CC/';

    private $autoUidModel;

    function __construct()
    {
        $this->autoUidModel         =   new AutoUidModel();
    }

    /**
     * 评论点滴
     * @param unknown $info
     * @return boolean
     */
    public function addComment ($info) {
        $data = $this->filterCommentDataOfMysql($info);
        if (strlen($data['content']) < 1){
            AC::$errorNo    =   'DCM001';
            return false;
        }

        $key   		=   $this->childCommentsKeyPrefix. $data['typeId'];
        $result		=   Storage::addData($key, $data);
        if (empty($result)) {
            AC::$errorNo    =   'DCM002';
            return  false;
        }

        return  $result;
    }

    /**
     * 获取单条子评论信息
     * @param $type
     * @param $typeId
     * @param $position
     * @return array|bool
     */
    public function getCommentInfo ($type, $typeId, $position) {
        if (empty($position) || empty($type) || empty($typeId)) {
            AC::$errorNo    =   'DCM001';
            return  FALSE;
        }

        $key    =   $this->childCommentsKeyPrefix. $typeId;
        $where  =   array('position' => $position,'type' => $type, 'typeId' => $typeId);
        $result =   Storage::getConditionData($key, $where);

        return  $result;
    }

    /**
     * 删除单条评论
     * @param $type
     * @param $typeId
     * @param $position
     * @return bool|int|resource
     */
    public function removeChildComment($type, $typeId, $position) {
        if (empty($position) || empty($type) || empty($typeId)) {
            AC::$errorNo    =   'DCM001';
            return  FALSE;
        }

        $key    =   $this->childCommentsKeyPrefix. $typeId;
        $where  =   array('position' => $position,'type' => $type, 'typeId' => $typeId);
        $data   =   ['status' => 1];
        $result =   Storage::setConditionData($key, $data, $where);
        return  $result;
    }

    /**
     * 获取楼中楼评论列表
     * @param $type
     * @param $typeId
     * @param $parentId
     * @param $positionId
     * @return array|bool
     */
    public function getCommentsList($type, $typeId, $parentId, $positionId = 0) {
        if (empty($parentId) || empty($type) || empty($typeId)) {
            AC::$errorNo    =   'DCM001';
            return  FALSE;
        }

        $start  =   0;
        $length =   AC::COMMENT_LIST_DISPLAY_LEN;
        $orderBy=   ' created_at ASC ';
        $key    =   $this->childCommentsKeyPrefix. $typeId;
        $where  =   'status = 0 AND type=' . $type . ' AND typeId=' . $typeId . ' AND parentId=' . $parentId;
        if(intval($positionId) > 0){
            $where .= ' AND position > ' . $positionId;
        }
        $result =   Storage::getConditionManyData($key, $where, $orderBy, $start, $length);

        return  $result;
    }


    /**
     * 过滤评论数据
     * @param $param
     * @return array
     */
    public function filterCommentDataOfMysql ($param) {
        $data = array(
            'uid'            =>  intval($param['uid']),
            'type'           =>  intval($param['type']),
            'typeId'         =>  intval($param['typeId']),
            'content'        =>  UK::getstr($param['content'], AC::TOPIC_BODY_LENGTH),
            'parentId'       =>  intval($param['parentId']),
            'rUid'           =>  intval($param['rUid']),
            'nickname'       =>  UK::getstr($param['nickname'], AC::NICKNAMELENGTH, '', 1),
            'rNickname'      =>  UK::getstr($param['rNickname'], AC::NICKNAMELENGTH, '', 1),
            'extend'         =>  isset($param['extend']) ? $param['extend'] : '',
            'created_at'     =>  AC::SERVER_TIME,
            'updated_at'     =>  AC::SERVER_TIME,
        );

        return $data;
    }

    /**
     * 从扩展字段中获取子评论
     * @param $extend
     * @param $type
     * @return array|object
     */
    public function getCommentOfComment ($extend, $type = 0) {
        $extend_arr = array();
        if(!empty($extend)) {
            $extend_arr = json_decode($extend, true);
        }
        $commentList = array();
        //楼中楼的评论数
        $count = isset($extend_arr['commentTimes']) ? $extend_arr['commentTimes'] : 0;
        if ($extend_arr['childComment']){
            $comment = $extend_arr['childComment'];
            foreach ($comment as $value){
                $user = $this->autoUidModel->getUserInfo($value['uid']);
                $nickname = $user['nickname'];
                $user['nickname'] = $this->autoUidModel->getRemarkName($user);
                $commentList[] = array(
                    "uid"           => strval($value['uid']),
                    'nickname'      => strval($nickname),
                    "position"      => $type ? strval($value['position'] - 1) : strval($value['position']),
                    "positionId"    => strval($value['positionId']),
                    "content"       => Emotion::emotionToCharacter($value['content']),
                    "created_at"    => strval($value['created_at']),
                    "user"          => $user
                );
            }
        }
        //如果附加字段中无评论怎么处理评论数
//        $count = $commentList ? $count : 0;
        $commentList = $commentList ? $commentList : (Object) array();
        if($count < 1){
            return (Object)array();
        }
        $count = $count - 3;
        $count = $count > 0 ? $count : 0;
        return ['commentList' => $commentList, 'counts' => $count];
    }


}